package com.cn.jx.manager.service.mtn.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.ContactRepository;
import com.cn.jx.manager.dao.MessageRepository;
import com.cn.jx.manager.dao.MessageUserRepository;
import com.cn.jx.manager.po.Contact;
import com.cn.jx.manager.po.Message;
import com.cn.jx.manager.po.MessageUser;
import com.cn.jx.manager.po.User;
import com.cn.jx.manager.service.mtn.IMessageServiceMtn;
import com.cn.jx.manager.service.query.IUserServiceQry;
import com.jx.enumType.MessageMethodEnum;
import com.jx.enumType.MessageStatusEnum;
import com.jx.enumType.MessageTypeEnum;

import com.jx.util.MapObject;
import com.jx.util.MapUtil;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月24日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("messageServiceMtn") 
@Transactional
public class MessageServiceImplMtn  extends BaseService  implements IMessageServiceMtn {

	@Autowired
	private MessageRepository messageDao;
	
	@Autowired
	private MessageUserRepository messageUserDao;
	
	@Autowired
	private IUserServiceQry userServiceQry;
	
	@Autowired
	private ContactRepository contactDao;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void sendMessage(MapObject mp) throws Exception {
		String title=MapUtil.getStringVal(mp, "title");
		String content=MapUtil.getStringVal(mp, "content");
		String operator=MapUtil.getStringVal(mp, "operator");
		String sender=MapUtil.getStringVal(mp, "sender");
		String msgType=MapUtil.getStringVal(mp, "msgType");
		List<String> receivers= MapUtil.getListVal(mp, "receivers");//接收人id集合
		List<String> orgs= MapUtil.getListVal(mp, "orgs");//接收部门id集合
		
		Message mesg=Message.getInstance();
		mesg.setTitle(title);
		mesg.setType(msgType);
		mesg.setContent(content);
		Message nmsg=messageDao.save(mesg);
		MessageUser msender=MessageUser.getInstance(operator);
		msender.setMessageId(nmsg.getId());
		msender.setReceivMethod(MessageMethodEnum.SEND.getCode());//发送
		msender.setReceivStatus(MessageStatusEnum.SEND.getCode());//已发送
		msender.setSender(sender);
		
		MessageTypeEnum msgenum=MessageTypeEnum.getMessage(msgType);
		
		/*获取部门下所有人 */
		if(orgs.size()>0){
			List<String> users=userServiceQry.findUserByOrgId(orgs);
			if(users!=null && users.size()>0){
				receivers.addAll(users);
			}
		}
		
		//循环接收人
		if(receivers.size()>0){
			receivers.forEach(rec->{
				//站内信接收
				MessageUser mrec=MessageUser.getInstance(rec);
				mrec.setMessageId(nmsg.getId());
				mrec.setReceivMethod(MessageMethodEnum.RECEIVE.getCode());//接收
				mrec.setReceivStatus(MessageStatusEnum.UNREAD.getCode());//未读
				mrec.setSender(sender);
				messageUserDao.save(mrec);
				
				if(!MessageTypeEnum.MSG.equals(msgenum)){
					User u=userServiceQry.getUserById(rec);
					String contactId=u.getContactId();//获取联系人ID
					if(Utility.isNotBlank(contactId)){
						Contact contact = contactDao.findEntityByPkid(contactId);
						String email=contact.getEmail();
						String mobile=contact.getMobile();
						
						switch (msgenum) {
						case EMAIL:
							//发送邮件
							if(Utility.isNotBlank(email)){
								try {
									MailUtils.sendMail(email, sender, title, content);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							break;
						case SMS:
							//发送短信
							if(Utility.isNotBlank(mobile)){
							}
							break;
						case ALL:
							//发送邮件
							if(Utility.isNotBlank(email)){
								try {
									MailUtils.sendMail(email, sender, title, content);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							//发送短信
							if(Utility.isNotBlank(mobile)){
							}
							break;
						default:
							break;
						}
						
						
					}
				}
			});
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void readMessage(String userId, String msgId) {
		messageUserDao.updateInboxRead(msgId, new Date());
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void delMessage(List<String> ids, String opertor) {
		
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeMessage(List<String> ids, String opertor) throws Exception {
		
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void recoverMessage(List<String> ids, String opertor) throws Exception {
		
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeMessageAll(String opertor) throws Exception {
		
	}

}
